﻿using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using ReflectionStudio.Core.Events;
using Word = Microsoft.Office.Interop.Word;
namespace ReflectionStudio.Core
{
    public class MSWordAgent
    {
        public string FullName
        {
            get
            {
                return aDoc.FullName;
            }
        }
        public Word.Application WordApp
        {
            get
            {
                return wordApp;
            }
        }
        Word.Application wordApp = null;
        public Word.Document ActiveDoc
        {
            get
            {
                return aDoc;
            }
        }
        Word.Document aDoc = null;
        Hashtable myHashtable = new Hashtable();
        public MSWordAgent()
        {
            CheckWordProcesses();
            this.OpenApp();
        }
        public void Close()
        {
            KillWord();
        }
        public void Save()
        {
            aDoc.Save();
        }
        public void Open(object filePath)
        {
            try
            {
                object readOnly = false;
                object isVisible = false;
                object missing = Missing.Value;
                //  make visible Word application
                
                //  open Word document named temp.doc
                aDoc = wordApp.Documents.Open(ref filePath, ref missing,
                    ref readOnly, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref isVisible, ref missing, ref missing,
                    ref missing, ref missing);
                aDoc.Activate();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public string Read()
        {
            string result = null;
            try
            {
                string fileContent = aDoc.Content.Text;   //这里读取所有的WORD
                result = fileContent;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        private void OpenApp()
        {
            wordApp = new Word.Application();
            wordApp.Visible = true;

        }
        public void CreateDoc(string filepath, string standard, string docname)
        {
            //  create offer letter
            try
            {
                //  Just to kill WINWORD.EXE if it is running
                //  copy letter format to temp.doc
                //filepath=D:\ZZZDBank\tfcIntexReport\Exe\Report\JHT\EmailA.docx
                //standard=JHT\EmailA.docx
                //docname=报价\Q20120907.docx
                string newfile = filepath.Replace(standard, docname);
                File.Copy(filepath, newfile, true);
                object filename = newfile;
                Open(filename);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void ReplacebyField(string field, string content)
        {
            //  Call FindAndReplace()function for each change
            //this.FindAndReplace(wordApp, "<Date>", dtpDate.Text);
            if (aDoc == null)
                return;
            this.FindAndReplace(wordApp, field, content);
        }
        private void CheckWordProcesses()
        {
            Process[] AllProcesses = Process.GetProcessesByName("WINWORD");
            myHashtable = new Hashtable();
            int iCount = 0;

            foreach (Process process in AllProcesses)
            {
                myHashtable.Add(process.Id, iCount);
                iCount = iCount + 1;
            }
        }
        public void KillWord()
        {
            Process[] AllProcesses = Process.GetProcessesByName("WINWORD");

            // check to kill the right process
            foreach (Process process in AllProcesses)
            {
                if (myHashtable.ContainsKey(process.Id) == false)
                    process.Kill();
            }
            AllProcesses = null;
        }
        private void FindAndReplace(Word.Application wordApp,
            object findText, object replaceText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;
            wordApp.Selection.Find.Execute(ref findText, ref matchCase,
                ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
                ref matchAllWordForms, ref forward, ref wrap, ref format,
                ref replaceText, ref replace, ref matchKashida,
                        ref matchDiacritics,
                ref matchAlefHamza, ref matchControl);
        }
    }
}
